﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Collections;
using com.code.Editors.Code;

namespace com.Editors
{
    public partial class UITableRelation : Form
    {
        ArrayList tables = null;
        IServiceProvider provider = null;
        IDictionaryService dictionaryService = null;
        private ArrayList tablesJoin = new ArrayList();
        private ArrayList cmbJoin = new ArrayList();

        public UITableRelation(IServiceProvider provider)
        {
            InitializeComponent();
            this.provider = provider;
            //int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
            //panel1.Padding = new Padding(0, 0, vertScrollWidth, 0);
            //panel1.AutoSizeMode = AutoSizeMode.GrowOnly;
        }

        public void setUITableRelation(ArrayList tables)
        {
            FlowLayoutPanel pagePanel = new FlowLayoutPanel();
            pagePanel.FlowDirection = FlowDirection.TopDown;
            pagePanel.Dock = DockStyle.Fill;
            this.tables = tables;
            
            for(int i = 0; i < tables.Count - 1; i++ )
            {
                GuidDataTable table = (GuidDataTable) tables[i];
                com.Editors.UserControls.ucTableRelation tblRelObj =
                                    new com.Editors.UserControls.ucTableRelation();
                if (i == 0)
                    tblRelObj.IsFirst = true;
                else
                    tblRelObj.IsFirst = false;
                tablesJoin.Add(tblRelObj);
                tblRelObj.setDataTables(this.tables);
                ComboBox cmb = new ComboBox();
                cmb.Name = "cmb" + i;
                cmb.Items.Add("INNER JOIN");
                cmb.Items.Add("LEFT JOIN");
                cmb.Items.Add("RIGHT JOIN");
                cmb.Items.Add("CROSS JOIN");
                cmb.SelectedIndex = 0;
                cmbJoin.Add(cmb);
                pagePanel.Controls.Add(tblRelObj);
                if(i != tables.Count - 2)
                    pagePanel.Controls.Add(cmb);
            }
             panel1.Controls.Add(pagePanel);                        
        }

        public ArrayList GetSelectedTables()
        {
            ArrayList tmpList = new ArrayList();
            foreach (com.Editors.UserControls.ucTableRelation tblRelObj in tablesJoin)
            {
                if(tblRelObj.ParentTable != null )
                    tmpList.Add(tblRelObj.ParentTable);
                if (tblRelObj.ChildTable != null)
                    tmpList.Add(tblRelObj.ChildTable);
            }
            return tmpList;
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            string data = "";
            string parentAlias = "";
            string childAlias = "";
            int i = 0;
            if (tables.Count == 1)
            {                
                data = ((GuidDataTable) tables[0]).TableName;
            }
            else
            {
                foreach (com.Editors.UserControls.ucTableRelation tblRelObj in tablesJoin)
                {
                    var tblParentObj = from GuidDataTable pTbl in tables
                                       where pTbl.TableName == tblRelObj.ParentTable
                                       select pTbl;

                    var tblChildObj = from GuidDataTable cTbl in tables
                                      where cTbl.TableName == tblRelObj.ChildTable
                                      select cTbl;

                    foreach (GuidDataTable objPTbl in tblParentObj)
                        parentAlias = objPTbl.TableAlias;
                    foreach (GuidDataTable objCTbl in tblChildObj)
                        childAlias = objCTbl.TableAlias;

                    if (i > 0)
                    {
                        int j = 0;
                        foreach (ComboBox cmb in cmbJoin)
                        {
                            if (i == j++)
                                data += cmb.Text + " ";
                        }
                        /**Control[] ct = panel1.Controls.Find("cmb" + i, true);
                        if (ct.Length > 0 && ct[0] != null)
                        {
                            if (((ComboBox)ct[0]).Text != "")
                                data += ((ComboBox)ct[0]).Text + " ";                        
                        }*/

                    }
                    if (parentAlias == "" || childAlias == "")
                    {
                        MessageBox.Show("Please select all required Join combinations.");
                        return;
                    }
                    if (tblRelObj != null && tblRelObj.ParentTable != null)
                    {
                        data += tblRelObj.ParentTable + " " + parentAlias + " " + tblRelObj.JoinType + " ";
                        if (i == 0)
                            data += tblRelObj.ChildTable + " " + childAlias + " ON \n";

                        data += parentAlias + "." + tblRelObj.ParentCol + " = " +
                        childAlias + "." + tblRelObj.ChildCol + "\n";
                    }
                    i++;
                }
            }

            dictionaryService = provider.GetService(typeof(IDictionaryService)) as IDictionaryService;
            dictionaryService.SetValue("TablesJoin", data);
        }

                
    }
}
